package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author rookie001
 * @creat 2023-06-07-16:59
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    /*图片上传*/
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //数据校验
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String s = null;
        try {
            //上传图片到minio                           要替换的字符值 字符值的替换
            String filename = UUID.randomUUID().toString().replace("-", "");
            //返回原始文件名  文件前缀
            String originalFilename = multipartFile.getOriginalFilename();
            //文件后缀
            String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //返回路径
            s = fileStorageService.uploadImgFile("", filename + postfix, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        //保存数据到wm_material
        WmMaterial wmMaterial = WmMaterial.builder()
                .userId(WmThreadLocalUtils.getUser().getId())
                .url(s)
                .createdTime(new Date())
                .type((short) 0)
                .isCollection((short) 0)
                .build();
        save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    /*素材列表查询*/
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //数据校验
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断是否收藏
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        //根据用户查询
        lambdaQueryWrapper.eq(WmMaterial::getUserId, WmThreadLocalUtils.getUser().getId());
        //按照时间倒叙
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //查询
        page = page(page, lambdaQueryWrapper);
        //返回结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /*删除图片*/
    @Override
    public ResponseResult delectPc(Integer id) {
        //数据校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据处理
        //根据id查询图片
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(removeById(id));
    }

 /*  收藏图片*/
    @Override
    public ResponseResult collect(Integer id) {
        //数据校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据处理
        //根据id查询图片
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmMaterial.setIsCollection((short) 1);
        //结果返回
        return ResponseResult.okResult(updateById(wmMaterial));

    }
}
